﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Microsoft.Reputation.Tests.FrontEndCommon
{
    public class DataBlock
    {
        public DataBlockHeader header;
        public byte[] shas;
        public byte[] url;

        public DataBlock(bool setDefaults)
        {
            header = new DataBlockHeader(setDefaults);
            if (setDefaults)
            {
                SetDefaults();
            }
        }

        public void SetDefaults()
        {
            header.SetDefaults();

            byte[] defaultSHA = {218,220,54,133,103,13,137,170,153,225,202,197,162,161,50,99,40,98,42,22,46,251,102,55,33,101,145,143,40,223,60,45};
            shas = defaultSHA;

            byte[] defaultUrl = { 104, 0, 116, 0, 116, 0, 112, 0, 58, 0, 47, 0, 47, 0, 97, 0, 46, 0, 99, 0, 111, 0, 109, 0, 0, 0 }; //http://a.com.
            url = defaultUrl;
        }

        public byte[] CreateDataBlock()
        {
            byte[] dbHeader = header.CreateDataBlockHeader();
            int index = 0;

            byte[] result = new byte[header.Size + shas.Length + url.Length];
            Array.Copy(dbHeader, 0, result, index, dbHeader.Length);
            index += dbHeader.Length;

            Array.Copy(shas, 0, result, index, shas.Length);
            index += shas.Length;

            Array.Copy(url, 0, result, index, url.Length);
            index += url.Length;

            Console.WriteLine("Index after creating DataBlock is {0}", index);

            return result;
        }
    }

    public class DataBlockHeader
    {
        public byte[] dwSig;
        public byte[] cbHeader;
        public byte[] dwFlags;
        public byte[] wClientMajor;
        public byte[] wClientMinor;
        public byte[] wClientBuild;
        public byte[] wClientQfe;
        public byte[] ceVariants;
        public byte[] ceHashes;
        public byte[] cbHash;
        public byte[] cbStrData;

        int size;

        public int Size
        {
            get
            {
                return dwSig.Length + cbHeader.Length + dwFlags.Length + wClientMajor.Length + wClientMinor.Length + wClientBuild.Length + wClientQfe.Length 
                    + ceVariants.Length + ceHashes.Length + cbHash.Length + cbStrData.Length;
            }
            set
            {
                size = value;
            }
        }

        public DataBlockHeader(bool setDefaults)
        {
            if (setDefaults)
            {
                SetDefaults();
            }
        }
    
        public void SetDefaults()
        {
            byte[] defaultSig = {byte.Parse("82"), byte.Parse("82")};
            dwSig = defaultSig;

            cbHeader = BitConverter.GetBytes((short)24);
            dwFlags = BitConverter.GetBytes((int)0);

            FileVersionInfo msasapi = FileVersionInfo.GetVersionInfo("msasapi.dll");    // getting the version of the current msasapi.dll,
            string version = msasapi.FileVersion;                                       // and setting that as the default ClientVersion.
            string[] verParts = version.Split('.');

            wClientMajor = BitConverter.GetBytes(short.Parse(verParts[0]));
            wClientMinor= BitConverter.GetBytes(short.Parse(verParts[1]));
            wClientBuild = BitConverter.GetBytes(short.Parse(verParts[2]));
            wClientQfe = BitConverter.GetBytes(short.Parse(verParts[3]));

            ceVariants = BitConverter.GetBytes((short)1);
            ceHashes = BitConverter.GetBytes((short)1);
            cbHash = BitConverter.GetBytes((short)32);
            cbStrData = BitConverter.GetBytes((short)26);   // Length of http://a.com + 1 for Null Terminator.
        }

        public byte[] CreateDataBlockHeader()
        {
            byte[] header = new byte[Size];
            int index = 0;
            Array.Copy(dwSig, 0, header, index, dwSig.Length);
            index += dwSig.Length;
            Array.Copy(cbHeader, 0, header, index, cbHeader.Length);
            index += cbHeader.Length;
            Array.Copy(dwFlags, 0, header, index, dwFlags.Length);
            index += dwFlags.Length;
            Array.Copy(wClientMajor, 0, header, index, wClientMajor.Length);
            index += wClientMajor.Length;
            Array.Copy(wClientMinor, 0, header, index, wClientMinor.Length);
            index += wClientMinor.Length;
            Array.Copy(wClientBuild, 0, header, index, wClientBuild.Length);
            index += wClientBuild.Length;
            Array.Copy(wClientQfe, 0, header, index, wClientQfe.Length);
            index += wClientQfe.Length;
            Array.Copy(ceVariants, 0, header, index, ceVariants.Length);
            index += ceVariants.Length;
            Array.Copy(ceHashes, 0, header, index, ceHashes.Length);
            index += ceHashes.Length;
            Array.Copy(cbHash, 0, header, index, cbHash.Length);
            index += cbHash.Length;
            Array.Copy(cbStrData, 0, header, index, cbStrData.Length);
            index += cbStrData.Length;

            Console.WriteLine("Index after creating DataBlock Header is {0}", index);

            return header;
        }
    }
}
